//
//  SubscriptVC.swift
//  swiftDemo
//
//  Created by mac on 2017/3/20.
//  Copyright © 2017年 毕小强. All rights reserved.
//

import UIKit

class SubscriptVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        struct TimesTable{
            let multiplier :Int
           subscript(index:Int)->Int{
                set (newValue){
                    print(newValue)
                }
                
                get{
                    return multiplier*index
                }
            }
        }
        var threeTimes = TimesTable(multiplier:3)
        print("six times three is \(threeTimes[6])")
        threeTimes[6] = 11
        
        struct Matrix{
            let rows:Int,columns:Int
            var grid:[Double]
            init(rows:Int,columns:Int) {
                self.rows = rows
                self.columns = columns
                
                grid = Array(repeatElement(0.0, count: rows*columns))
            }
            
            func indexIsValdForRow(row:Int,colimn:Int) -> Bool {
                return row >= 0 && row < rows && colimn >= 0 && colimn < columns
            }
            
            subscript(row:Int,column:Int)->Double{
                get{
                    assert(indexIsValdForRow(row: row, colimn: column),"Index out of range")
                    return grid[(row*column)+column]
                }
                
                set{
                    assert(indexIsValdForRow(row: row, colimn: column),"Index out of range")
                    grid[(row*columns)+column] = newValue
                }
            }
        }
        
        var matrix = Matrix(rows:2,columns:3)
        matrix[0,1] = 1.5
        matrix[1,0] = 3.2
        print(matrix[0,0])
        
        
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
